// 04jyhs/accountAdmin/rechange/rechange.js
var co = require('../../../resource/js/common.js')
var app = getApp()
var _formId
var _appid = app.Data.app.appid
var _this, _data
Page({
   data: {
      inputMoney: '', //输入的金额
      inputStr: '', //输入金额(字符串)       
   },

   onLoad: function (options) {
      _this = this
      _data = this.data
      console.log(app.Data)
   },

   //输入金额
   inputMoney: function (e) {
      var money = parseFloat(e.detail.value);
      var inputStr = e.detail.value
      this.setData({
         inputMoney: money,
         inputStr: co.filterStr(inputStr, '.'),
      })
   },
   //充值
   recharge: function (e) {
      _formId=e.detail.formId
      var money = e.detail.value.inputMoney
      if (co.empty(_data.inputMoney) || _data.inputMoney <= 0) {
         co.toastAllText('输入的充值金额不正确!')
         return
      }
      _this.setData({
         inputMoney: co.round(money, 2),
         inputStr: co.roundStr(money, 2),
      })
      //_this.rechangePayCord()
      _this.pay()
   },
   //支付(充值)
   pay: function () {
      var appid = app.Data.app.appid
      co.getShareApi('api/Wxpay/createParam', {
         openid: app.Data.userInfo.Wx_Openid[appid],
         appid: appid,
         // key: 'bShYjNaUyIzGnEhSiAzIlIsNaHuOhZuF',
         // notify_url: 'http://api.igoyp.com/public/weixinpay/notify.php',
         // mch_id: '1514732771',
         money: _data.inputMoney,
         abstract: '充值'
      }, function (res) {
         if (res.code == 0) {
            _this.bindCallback(res)
         } else {
            co.toastAllText(res.errMsg)
         }
      })
   },
   //绑定微信支付回调监听
   bindCallback: function (result) {
      result.data.success = function (res) {
         console.log('【调起支付res】', res)
         _this.sendwxProjectminiMsg(_formId)
         _this.sendShortMessage()
         _this.sendTemplateMsg()
         _this.rechangePayCord()
      }
      result.data.fail = function (res) {
         console.log('【调起支付失败res】', res)
         co.toastAllText(res.msg)
         //wx.hideLoading()
      }
      //调起微信支付
      wx.requestPayment(result.data)
   },
   //发送小程序模板消息
   sendwxProjectminiMsg(formId) { 
      co.getShareApi("api/Weixin/sendWxAppTemplateMsg",{
         appid:_appid,
         secret:app.Data.app.secret,
         openid:app.Data.userInfo.Wx_Openid[_appid],
         template_id: "cHo9uzKIUVLzvsnoWajqMMy9asE1QtQBsUyC2jo5g5o",
         page:'04jyhs/accountAdmin/accountAdmin',
         formId: formId,
         keyword1: _data.inputMoney + "元",
         keyword2: "已支付",
         keyword3: co.getFormatDate(2),
         keyword4: "充值未到账可联系18006938850",
         keyword5: Date.parse(new Date()),
         keyword6: "微信",
      },function(res){
         if(res.code == 0){
             console.log(res.data)
         }
      })
   },
   //发送短信模板
   sendShortMessage() {
      co.getShareApi("api/Sms/sendShortMessage",{
         mobile: app.Data.userInfo.User_Mobile,
         value: JSON.stringify([_data.inputMoney]),
         type: "recharge",
         appid:"1400134010"
      },function(res){
         if(res.code == 0){
            console.log(res.msg)
         }
      })
   },
   //发送公众号模板消息
   sendTemplateMsg() {
      co.getShareApi("api/Weixin/sendTemplateMsg",{
         appid: _appid,
         secret: app.Data.app.secret,
         openid: app.Data.userInfo.Wx_Openid[_appid],
         template_id: "2cN8gkpULAogfly1wZm0zwYWheJktni2pyGkpMGmIM",	
         key_title: "充值,#22cc22",	
         keyword1: "10,#22cc22",
         keyword2: "4,#22cc22",	
         keyword3: "3,#22cc22",
         key_remark: "充值成功",	
         toUrl: "04jyhs/accountAdmin/accountAdmin",	
      },function(res){
         if (res.code == 0) {
            console.log(res.data)
         } 
      })
   },
   //充值添加支付记录
   rechangePayCord() {
      var Type = app.Data.userInfo.User_Type
      var param={
         Abstract:"充值",
         Money: _data.inputMoney,
         Pay_From: "wx_pay",
         Pay_To:"91_pay",
      }
      if (Type == "jyhs_admin"){//总后台充值
         param.Biz_ID = app.Data.shopInfo.Biz_ID
         param.Payer = 'Biz_' + app.Data.shopInfo.Biz_ID
         co.getJyhsApi("money/Recordbiz/bizAdd", param,function(res){
              if(res.code == 0){
                 console.log(res.msg)
                 wx.navigateTo({
                    url: '/04jyhs/accountAdmin/accountAdmin',
                 })
              } else {
                 console.log(res.msg)
              } 
         })
      }else{//回收帮人员充值
         param.User_ID =app.Data.userInfo.User_ID
         param.Payer = 'User_' + app.Data.userInfo.User_ID
         co.getJyhsApi("money/Recorduser/userAdd", param, function (res) {
            if (res.code == 0) { 
               console.log(res.msg)
               wx.navigateTo({
                  url: '/04jyhs/accountAdmin/accountAdmin',
               })
            }else{
               console.log(res.msg)
            } 
         })
      }  
   },
})

